/* 
 * Copyright (C) 2012 UTSChedule
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package utsc.apps.utschedule.classes;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.j256.ormlite.dao.CloseableIterator;
import com.j256.ormlite.dao.ForeignCollection;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.field.ForeignCollectionField;
import com.j256.ormlite.table.DatabaseTable;

@DatabaseTable
public class Timetable implements Serializable {

	private static final long serialVersionUID = -1613261158629989398L;
	public static final String STATUS="_status";
	public static final String SESSION="_session";
	
	@DatabaseField(generatedId=true)
	private int _timetableID;
	@DatabaseField
	private String _title;
	@DatabaseField
	private String _session;
	@DatabaseField
	private int _year;
	@DatabaseField
	private Date _dayCreated;
	@DatabaseField
	private Date _dayUpdated;
	@DatabaseField
	private long _startdate;
	@DatabaseField
	private int _status=STATUS_ACTIVE;
	public static final int STATUS_ACTIVE=1;
	public static final int STATUS_INACTIVE=0;
	@ForeignCollectionField(eager=false,columnName="courses")
	private ForeignCollection<Course> _courses;
	
	private ArrayList<Course> _courseList;
	private Calculator _gpaCalculator;

	public int get_timetableID() {
		return _timetableID;
	}

	public void set_timetableID(int _timetableID) {
		this._timetableID = _timetableID;
	}

	public String get_title() {
		return _title;
	}

	public void set_title(String _title) {
		this._title = _title;
	}

	public String get_session() {
		return _session;
	}

	public void set_session(String _session) {
		this._session = _session;
	}

	public int get_year() {
		return _year;
	}

	public void set_year(int _year) {
		this._year = _year;
	}

	public Date get_dayCreated() {
		return _dayCreated;
	}

	public void set_dayCreated(Date _dayCreated) {
		this._dayCreated = _dayCreated;
	}

	public Date get_dayUpdated() {
		return _dayUpdated;
	}

	public void set_dayUpdated(Date _dayUpdated) {
		this._dayUpdated = _dayUpdated;
	}

	public int get_status() {
		return _status;
	}

	public void set_status(int _status) {
		this._status = _status;
	}

	public void addCourse(Course c) {
		_courses.add(c);
	}
	
	public void set_courses(ForeignCollection<Course> _courses) {
		this._courses = _courses;
	}
	
	public ArrayList<Course> get_courseList() {
		if (_courseList==null||_courseList.isEmpty()) {
			refreshCourseList();
		}
		return _courseList;
	}
	
	public void refreshCourseList() {
		try {
			if (_courses!=null) {
				CloseableIterator<Course> iterator=_courses.closeableIterator();
				if (_courseList==null) {
					_courseList=new ArrayList<Course>();
				}else {
					_courseList.clear();
				}
				try {
					while (iterator.hasNext()) {
						_courseList.add(iterator.next());
					}
				} finally{
					iterator.close();
				}
			}else {
				if (_courseList==null) {
					_courseList=new ArrayList<Course>();
				}else {
					_courseList.clear();
				}
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public String toString() {
		return _title;
	}
	
	public Timetable(){
	}

	public long get_startdate() {
		return _startdate;
	}

	public void set_startdate(long _startdate) {
		this._startdate = _startdate;
	}

	public void set_gpaCalculator(List<Course> courses) {
		if (_gpaCalculator==null) {
			
			_gpaCalculator=new Calculator(courses);
		}
		else {
			_gpaCalculator.setCourses(new ArrayList<Course>(courses));
		} 
	}
	
	public Calculator get_gpaCalculator() {
		return _gpaCalculator;
	}
	
}
